#include "Sema.h"
#include "ScopedLock.h"

Sema::Sema(int initialValue) {
	mValue = initialValue;
}

void Sema::wait() {
	ScopedLock scopedLock(&mLock);
	while (mValue < 1) {
		mCondition.wait(&mLock);
	}
	--mValue;
}

void Sema::post() {
	ScopedLock scopedLock(&mLock);
	++mValue;
	mCondition.signal();
}

int Sema::getValue() {
  return mValue;
}
